<?php

namespace app\manage\controller;

use app\common\validate\merchant\MerchantValid;
use app\manage\loader\ManageServices;
use think\Exception;

/**
 * 商户管理
 */
class Merchant extends BaseApi
{


    /**
     * 添加商户
     * @return void
     */
    public function create(): void
    {
        $postData = input('post.');
        try {
            validate(MerchantValid::class)->scene('create')->check($postData);
            $result = ManageServices::Merchant()->create($postData, $this->opUser);
            api_result(API_SUCCESS, '创建成功', $result);
        } catch (Exception $e) {
            api_result($e);
        }
    }

    /**
     * 重置密码
     * @return void
     */
    public function reset_pass(): void
    {
        $postData = input('post.');
        try {
            validate(MerchantValid::class)->scene('resetManagerPasswd')->check($postData);
            $result = ManageServices::Merchant()->resetPasswd($postData['mer_id'], $postData['password'], $this->opUser);
            api_result(API_SUCCESS, '操作成功', $result);
        } catch (Exception $e) {
            api_result($e);
        }
    }

    /**
     *  修改商户的状态
     * @return void
     */
    public function edit_status(): void
    {
        $postData = input('post.');
        try {
            validate(MerchantValid::class)->scene('editMerchantStatus')->check($postData);
            $result = ManageServices::Merchant()->editStatus($postData['mer_id'], $postData['status'], $this->opUser);
            api_result(API_SUCCESS, '操作成功', $result);
        } catch (Exception $e) {
            api_result($e);
        }
    }
    /**
     *  修改商户归属
     * @return void
     */
    public function edit_affiliation(): void
    {
        $postData = input('post.');
        try {
            validate(MerchantValid::class)->scene('edit_affiliation')->check($postData);
            ManageServices::Merchant()->editFiled($postData['mer_id'],$postData['op_user']);
            api_result(API_SUCCESS, '操作成功',['mer_id' => $postData['mer_id']]);
        } catch (Exception $e) {
            api_result($e);
        }
    }

    public function edit_endTime(): void
    {
        $postData = input('post.');
        try {
            validate(MerchantValid::class)->scene('editEndTime')->check($postData);
            $result = ManageServices::Merchant()->editMerchantInfo($postData['mer_id'], $postData);
            api_result(API_SUCCESS, '操作成功', $result);
        } catch (Exception $e) {
            api_result($e);
        }
    }

    /**
     * 格式化where条件
     * @param $params
     * @return array
     */
    protected function formatWhere($params): array
    {
        $map = [];

        if (!empty($params['keyword'])) {
            $keyword = trim($params['keyword']);
            if (isMobile($keyword)) {
                $map[] = ['mobile', '=', $keyword];
            } else {
                $map[] = ['title|contact', 'like', '%' . $keyword . '%'];
            }
        }
        return $map;
    }

    /**
     * 商户列表
     * @return void
     */
    public function page(): void
    {
        $getData = input('get.');
        try {
            $option = $this->formatOption($getData);
            $option['where'] = $this->formatWhere($getData);
            $pageList = ManageServices::Merchant()->page($option);
            api_result(API_SUCCESS, '操作成功', $pageList);
        } catch (Exception $e) {
            api_result($e);
        }
    }

    /**
     * 商户列表无分页
     * @return void
     */
    public function list(): void
    {
        $getData = input('get.');
        try {
            $option = $this->formatOption($getData);
            $option['where'] = $this->formatWhere($getData);
            $pageList = ManageServices::Merchant()->list($option);
            api_result(API_SUCCESS, '操作成功', $pageList);
        } catch (Exception $e) {
            api_result($e);
        }
    }

    /**
     * 商户详情
     * @return void
     */
    public function info(): void
    {
        $getData = input('get.');
        try {
            if (empty($getData['mer_id'])) {
                api_result(API_ERROR, "商户编号不能为空");
            }
            $info = ManageServices::Merchant()->info($getData['mer_id']);
            api_result(API_SUCCESS, '操作成功', $info);
        } catch (Exception $e) {
            api_result($e);
        }
    }

    /**
     * 修改虚码数
     * @return void
     */
    public function save_stock(): void
    {
        $param = input('post.');
        try {
            $this->validate($param, MerchantValid::class . '.save_stock');
            ManageServices::Merchant()->saveStock($param['mer_id'], (int)$this->opUser, (int)$param['num'], (int)($param['type'] ?? 1));
            api_result(API_SUCCESS, '请求成功');
        } catch (Exception $e) {
            api_result($e);
        }
    }

    /**
     * 修改备注
     */
    public function edit_memo(): void
    {
        $postData = input('post.');
        try {
            validate(MerchantValid::class)->scene('index')->check($postData);
            $result = ManageServices::Merchant()->editMerchantInfo($postData['mer_id'], $postData);
            api_result(API_SUCCESS, '操作成功', $result);
        } catch (Exception $e) {
            api_result($e);
        }
    }
}
